/*******************************************************************************
 * Copyright (c) 2011 - 2012 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *     Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.revocation.wizard.panel;

import it.txt.access.capability.commons.filter.FilterXML;
import it.txt.access.capability.revocation.wizard.panel.controller.ControllerPanelAuthorisingCapabilitySelection;

import java.awt.Frame;
import java.io.File;

import javax.swing.JFileChooser;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT network.txtgroup.com)
 */
public class PanelAuthorisingCapabilitySelection extends javax.swing.JPanel {

    private static final long serialVersionUID = 2375224024379683976L;
    private ControllerPanelAuthorisingCapabilitySelection controller;
    private File currentAuCapTokenPath = null;
    private File currentAuCapToken = null;
    private JFileChooser delegationChooser;
    private FilterXML filterAssertion;
    private Frame myContainer;
    private final String title;
    
    public PanelAuthorisingCapabilitySelection(String title, Frame container, ControllerPanelAuthorisingCapabilitySelection cpat) {
        this.title = title;
        delegationChooser = new JFileChooser();
        initComponents();
        controller = cpat;
        myContainer = container;
        filterAssertion = new FilterXML("Access Rights Capability");
        delegationChooser.setFileFilter(filterAssertion);
        delegationChooser.setMultiSelectionEnabled(false);
        
        //init file chooser
        System.out.println(System.getProperty("user.dir"));
        if (currentAuCapTokenPath == null) {
            delegationChooser.setCurrentDirectory(new File("."));
        }
        else{
            delegationChooser.setCurrentDirectory(currentAuCapTokenPath);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        centerPanel = new javax.swing.JPanel();
        centerTitlePanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        welcomeLabel = new javax.swing.JLabel();
        delegationText1 = new javax.swing.JTextField();
        policyAccessRightsCapabilityIDButton1 = new javax.swing.JButton();
        delegatioNoteLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        delegationNotesArea2 = new javax.swing.JTextArea();
        jCheckBox1 = new javax.swing.JCheckBox();

        centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        centerTitlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(this.title));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("bundles/PanelAuthorisingCapabilitySelection"); // NOI18N
        welcomeLabel.setText(bundle.getString("subtitle")); // NOI18N

        delegationText1.setEditable(false);
        delegationText1.setToolTipText(bundle.getString("authorisingAuCapPathToolTip")); // NOI18N
        delegationText1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        delegationText1.setFocusable(false);
        delegationText1.setPreferredSize(new java.awt.Dimension(2, 20));

        policyAccessRightsCapabilityIDButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Open_16x16.png"))); // NOI18N
        policyAccessRightsCapabilityIDButton1.setText("Load");
        policyAccessRightsCapabilityIDButton1.setToolTipText(bundle.getString("loadButtonToolTip")); // NOI18N
        policyAccessRightsCapabilityIDButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyAccessRightsCapabilityIDButton1ActionPerformed(evt);
            }
        });

        delegatioNoteLabel1.setText(bundle.getString("loadingResultLabel")); // NOI18N

        delegationNotesArea2.setColumns(20);
        delegationNotesArea2.setEditable(false);
        delegationNotesArea2.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        delegationNotesArea2.setRows(5);
        delegationNotesArea2.setToolTipText("");
        jScrollPane3.setViewportView(delegationNotesArea2);

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Automatically set the revocation issuer");
        jCheckBox1.setToolTipText(bundle.getString("autoIssuerSettingCheckBoxToolTip")); // NOI18N

        javax.swing.GroupLayout centerTitlePanelLayout = new javax.swing.GroupLayout(centerTitlePanel);
        centerTitlePanel.setLayout(centerTitlePanelLayout);
        centerTitlePanelLayout.setHorizontalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addGroup(centerTitlePanelLayout.createSequentialGroup()
                        .addComponent(delegationText1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(policyAccessRightsCapabilityIDButton1))
                    .addGroup(centerTitlePanelLayout.createSequentialGroup()
                        .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addComponent(delegatioNoteLabel1))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(centerTitlePanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        centerTitlePanelLayout.setVerticalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(welcomeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(delegationText1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyAccessRightsCapabilityIDButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addGap(18, 18, 18)
                .addComponent(delegatioNoteLabel1)
                .addContainerGap(274, Short.MAX_VALUE))
            .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(centerTitlePanelLayout.createSequentialGroup()
                    .addGap(140, 140, 140)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        centerTitlePanel.getAccessibleContext().setAccessibleName("Authorising revocation selection");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void policyAccessRightsCapabilityIDButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyAccessRightsCapabilityIDButton1ActionPerformed
        int returnValue = delegationChooser.showOpenDialog(myContainer);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            currentAuCapToken = delegationChooser.getSelectedFile();
            if(controller.recoverAccessRightsCapability(currentAuCapToken)){
                delegationNotesArea2.setText(controller.getAccessRightsPublicValuesToString());
                delegationText1.setText(currentAuCapToken.getAbsolutePath());
                delegationText1.setBorder(
                        javax.swing.BorderFactory.createLineBorder(
                        new java.awt.Color(0, 0, 0)));
                currentAuCapTokenPath = currentAuCapToken.getParentFile();
                delegationNotesArea2.setCaretPosition(0);
            }
            else{
                delegationNotesArea2.setText(controller.recoverAccessRightsCapabilityErrorMessage());
                delegationText1.setBorder(
                        javax.swing.BorderFactory.createLineBorder(
                        new java.awt.Color(255, 0, 0)));
            }
        }
    }//GEN-LAST:event_policyAccessRightsCapabilityIDButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel centerTitlePanel;
    private javax.swing.JLabel delegatioNoteLabel1;
    private javax.swing.JTextArea delegationNotesArea2;
    private javax.swing.JTextField delegationText1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton policyAccessRightsCapabilityIDButton1;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables
    
    public boolean isAutomaticIssuerInsertionEnabled () {
        return this.jCheckBox1.isSelected();
    } 
}
